package SGUI

import (
	. "SGEngine2"
	"dom4g"
	//"strconv"
)

func GenImageByXML(elem *dom4g.Element, node *Node, tw *TopWidget, p IWidget) IWidget {
	newwidget := &Img{}
	newwidget.WidgetInfo.Node = node
	newwidget.Top = tw
	parseNodeAttrs(elem, newwidget, p)
	if v, ok := elem.AttrValue("src"); ok {
		newwidget.ImageName = v
	}

	newwidget.Color = Color4{1, 1, 1, 1}
	return newwidget
}

type Img struct {
	WidgetInfo
	ImageName string
	Color     Color4
}

func (this *Img) GetSize() (float32, float32) {
	return this.WidgetInfo.Width, this.WidgetInfo.Height
}

func (this *Img) OnAttach(node *Node) {
	this.WidgetInfo.Node = node
	this.Component2D.OnAttach(node)
	this.Component2D.Node.Scale = VecOne
	node.GetRender().Mat = this.Component2D.GenTextureMaterial(this.ImageName)
	b := node.GetRender().Mat.Tex.Texture.Bounds()
	this.Component2D.SetSize(float32(b.Dx()), float32(b.Dy()))
	node.GetRender().RenderMesh = this.Component2D.GenQuadMesh(&this.Color, true)

}

func (this *Img) GetWidgetInfo() *WidgetInfo {
	return &this.WidgetInfo
}

func (this *Img) OnUpdate() {

}
